package com.etalk.crm.controller;

import com.etalk.crm.dao.LinksMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;

/**
 * @author: Jordan
 * @date: 2019/1/22 14:56
 * @description:
 */
@RequestMapping("/s")
@Controller
public class ShortUrlController {

    @Resource
    private LinksMapper linksMapper;
    
    @GetMapping(value = "/{shortUrlCode}")
    public String redirectLongUrl(@PathVariable("shortUrlCode")String shortUrlCode){
        String longUrl=linksMapper.selectLongUrlByShortUrl(shortUrlCode);

        if (StringUtils.isNotEmpty(longUrl)){
            linksMapper.updateLinksViews(shortUrlCode);
            return "redirect:" + longUrl;
        }
        return "redirect:http://etalk365.cn";
    }
}
